package com.shwread.android.ui.dialog;

import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.TextView;

import com.shwread.android.qysw.activity.BaseBusinessActivity;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000204.R;

import java.util.List;

import logic.bean.BookBean;
import logic.beanenum.BookStatus_Enum;
import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.dao.BookDao;
import logic.util.FileUtil;
import logic.util.Util;

/**
 * 书籍删除对话框
 */
public class DeleteShelfCoverDialog extends Dialog {
    private TextView vContent;
    private TextView vOK;
    private TextView vCancel;
    private CheckBox cbDeleteFile;

    private BaseBusinessActivity activity;
    private BookBean bookMarkBean;
    private List<BookBean> allData;

    private BookDao dao;

    public DeleteShelfCoverDialog(MainActivity activity, BookBean bookMarkBean, List<BookBean> allData) {
        super(activity, R.style.common_dialog);
        this.activity = activity;
        this.allData = allData;
        this.bookMarkBean = bookMarkBean;
        dao = BookDao.getInstance(activity);
        findViews();
        setListeners();
        setCanceledOnTouchOutside(true);
        getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);
    }

    private void findViews() {
        View view = LayoutInflater.from(activity).inflate(R.layout.shelf_delete_dialog, null);

        vCancel = (TextView) view.findViewById(R.id.shelf_delete_cancel);
        vOK = (TextView) view.findViewById(R.id.shelf_delete_ok);
        vContent = (TextView) view.findViewById(R.id.shelf_delete_content);
        vContent.setText("文件不存在，请删除该封面");
        cbDeleteFile = (CheckBox) view.findViewById(R.id.shelf_delete_local_file);
        cbDeleteFile.setText("同时全部清理类似书籍");
        setContentView(view);
    }

    private void setListeners() {
        ClickListener l = new ClickListener();
        vOK.setOnClickListener(l);
        vCancel.setOnClickListener(l);

    }

    void clickOk() {
        if (cbDeleteFile.isChecked()) {
            clearCover();
        } else {
            bookMarkBean.syncStatus = BookBean.DELETE;
            if (Util.isEmpty(bookMarkBean.account)) bookMarkBean.account = Const.phone_number;
            dao.updateSyncBook(bookMarkBean);
        }
        activity.getUIHandler().sendEmptyMessage(DefaultConsts.DELETE_BOOKSHELF_BOOKS_OK);
        dismiss();
    }

    private void clearCover() {
        if (allData == null || allData.size() <= 1) return;
        for (BookBean item : allData) {
            String path = "";
            if (item.book_id == -1L && item.status == BookStatus_Enum.LOCAL.getValue()) {
                path = item.path;
            } else if (item.status == BookStatus_Enum.ONLINE.getValue()) {
                path = FileUtil.OnlinePath(String.valueOf(item.book_id)) + FileUtil.Book_CATALOGUE;
            } else if (item.status == BookStatus_Enum.DOWNLOAD.getValue()) {
                path = FileUtil.DownloadPath() + item.book_id + "/META-INF/book.ncx";
            }
            delete(path, item);
        }
    }

    private void delete(String path, BookBean bean) {
        if (!FileUtil.exists(path)) dao.delReadingBook(bean);
    }

    void clickCancel() {
        dismiss();
    }

    private class ClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.shelf_delete_ok:
                    clickOk();
                    break;
                case R.id.shelf_delete_cancel:
                    clickCancel();
                    break;
            }
        }
    }
}
